package com.itmumu.miniwechat.user.controller;

import com.itmumu.miniwechat.common.constants.RedisKey;
import com.itmumu.miniwechat.common.domain.response.ApiResult;
import com.itmumu.miniwechat.common.utils.RedisUtils;
import com.itmumu.miniwechat.user.domain.dto.SearchContactVo;
import com.itmumu.miniwechat.user.domain.vo.response.LoginQrcodeVo;
import com.itmumu.miniwechat.user.domain.vo.response.UserInfoVo;
import com.itmumu.miniwechat.user.service.UserService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/generateLoginQrcode")
    public ApiResult<LoginQrcodeVo> generateLoginQrcode() throws WxErrorException {
        LoginQrcodeVo loginQrcodeVo = userService.generateLoginQrcode();

        return  ApiResult.success(loginQrcodeVo);
    }

    @GetMapping("/generateEventCode")
    public ApiResult<LoginQrcodeVo> generateEventCode() throws WxErrorException {
        LoginQrcodeVo loginQrcodeVo = userService.generateEventCode();

        return  ApiResult.success(loginQrcodeVo);
    }


    @PostMapping("/qrcode/login")
    public  ApiResult<Map<String, String>> login(@RequestParam("ticket") String ticket) {
        HashMap<String, String> map = new HashMap<>();

        String accessToken = "";

        // 查询redis中，对应的ticket是否存在
        String hasTicket = RedisUtils.get(RedisKey.getKey(RedisKey.LOGIN_TICKET + ticket), String.class);
        if (hasTicket != null) {
            // 发放登录token
        }

        map.put("token", accessToken);
        return ApiResult.success(map);
    }


    @GetMapping("/getUserInfo")
    public ApiResult<UserInfoVo> getUserInfo(@RequestParam Integer uid) {
        UserInfoVo userInfoVo = userService.getUserInfo(uid);
        return  ApiResult.success(userInfoVo);
    }
}
